// pages/pay/pay.js
const app = getApp();
const api = require("../../utils/api.js").default;
const myRequest = require("../../utils/request.js").default;
 const token = wx.getStorageSync("token");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    head:"订单确认",
    kuaidi: [{ name: "平邮", price: 20 },{ name: "特快", price: 29 },{ name: "次日达", price: 32}],
    key:0,
    allcount:0,
    goodsList: [],
   province:[],
    customItem: '全部',
    proindex:"",
    city:[],
    cityod:[],
    cityindex:"",
    town:[],
    townindex:"",
    addressList:[],
    showDialog:"",
    addNew:true,
    items: [{ name: "true", value: "是" }, { name: "false", value: "否" }],
    xianshiaddress:[],
    addressKey:0,
    form:[]
    

    
    


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var token = wx.getStorageSync("token");
    var that=this;
    wx.request({
      url: 'https://nideshop.bluej.cn/api/cart/checkout',
    
      header:{
        'X-Nideshop-Token': token
      },
      data:{
        addressId:options.addressid,
        couponId: ""

      },
      success(res){
        console.log(res)
        var arr=[];
        arr.push(res.data.data.checkedAddress)
        that.setData({
          goodsList: res.data.data.checkedGoodsList,
          allcount: res.data.data.goodsTotalPrice,
          xianshiaddress:arr
        })
          
         
      }
    });
    wx.request({
      url:api.addressList,
      header:{
        'X-Nideshop-Token': token
      },
      success(res){
        
      }
    });
    wx.request({
      url: "https://nideshop.bluej.cn/api/region/list",
      header: {

        'X-Nideshop-Token': token
      },
      data: {
        parentId: 1
      },
      success(res) {
        
        var arr=[];
        for(var i=0;i<res.data.data.length;i++){
          arr.push(res.data.data[i].name)
        }
        that.setData({
          province:arr
        })
       
      }
    });
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  this.data.allcount=this.data.allcount + this.data.kuaidi[this.data.key].price;
    var that=this;
  
      
   
   

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  chooice(e){
    console.log(e.currentTarget)
    this.setData({
      key: e.currentTarget.dataset.index,

    })
  },
  count(){
    return this.data.allcount = this.data.allcount + this.data.kuaidi[this.data.key].price
  },
  submit(e){

    
    var that=this;
    var info=e.detail.value;
    var crr=[];
    crr.push(info)
    this.setData({
      form:crr
    })
    info.province=parseInt(info.province)+2;
    info.city = this.data.cityid[info.city];
    info.town =this.data.townid[info.town];
    console.log(info)
    if(info.city=="" || info.province=="" || info.town=="" || info.Name=="" || info.tel=="" ){
      wx.showToast({
        title: '请填写正确的信息',
        icon: 'loading',
        duration: 1000
      })
    }else{
    var token = wx.getStorageSync("token");
    wx.request({
      url: api.addresssave,
      method:"POST",
      header:{
        'X-NideShop-Token':token
      },
      data:{
        id:"",
        name:info.Name,
        mobile:info.tel ,
        province_id:info.province,
        city_id: info.city,
        district_id:info.town,
        address: info.address,
        is_default: true
      },
      success(res){
            console.log(res)
            var arr=[]
       that.data.addressList.push(res.data.data);
        arr = that.data.addressList
        that.setData({
          addressList: arr
        })

      }
    });
    }
 
  },
 provincechange: function (e) {
    console.log('picker发送选择改变，携带值为', e)
    var that=this;
    this.setData({
     proindex: e.detail.value
    });
    var id=parseInt(this.data.proindex)+2
   wx.request({
     url: "https://nideshop.bluej.cn/api/region/list",
     header: {

       'X-Nideshop-Token': token
     },
     data: {
       parentId: id
     },
     success(res) {
       console.log(res)
       var arr = [];
       var brr=[];
       for (var i = 0; i < res.data.data.length; i++) {
         arr.push(res.data.data[i].name)
         brr.push(res.data.data[i].id)
       }
       console.log(arr)
       that.setData({
         city: arr,
         cityid:brr
       })
       
     }
   })
  },
  citychange(e){
    var that=this;
    this.setData({
      cityindex: e.detail.value
    });
    var id=this.data.cityid[this.data.cityindex]
    console.log(id)
    if(this.data.proindex>=0){
      wx.request({
        url: "https://nideshop.bluej.cn/api/region/list",
        header: {
          'X-Nideshop-Token': token
        },
        data: {
          parentId: id
        },
        success(res) {
          console.log(res)
          var arr = [];
          var brr=[];
          for (var i = 0; i < res.data.data.length; i++) {
            arr.push(res.data.data[i].name)
            brr.push(res.data.data[i].id)
          }
          console.log(arr)
          that.setData({
            town: arr,
            townid:brr
          })

        }
      })
    }
   
  },
  townchange(e){
    this.setData({
      townindex: e.detail.value
    })
  },
  del(e){
   
    var that=this;
    wx.request({
      url: api.addressdelete,
      method:"POST",
      header:{
        'X-Nideshop-Token': token
      },
        data:{
          id:that.data.addressList[e.currentTarget.dataset.index].id
        },
        success(res){
          console.log(res)
        }
    });
    wx.request({
      url: api.addressList,
      header: {

        'X-Nideshop-Token': token
      },
      success(res) {
        console.log(res)
        that.setData({
          addressList: res.data.data
        })


      }
    })
  },
  toggleDialog(){
    if(this.data.addressList.length>0){
      this.setData({
        showDialog: true,

        addNew: true
      })
    }else{

     wx.showToast({
       title: '请先添加地址',
       icon:"loading",
       duration:1000
     })
    }
  
  },
  addnew(){
    this.setData({
      showDialog: true,
      addNew:false
    })
  },
  cancel(){
    this.setData({
      showDialog: false
    })
  },
  checkAddress(e){
    this.setData({
      addressKey: e.currentTarget.dataset.index
    })
   
  },
  pay(e){
    console.log('xianshiaddress',this.data.xianshiaddress)
    wx.request({
      url: api.ordersubmit,
      header: {

        'X-Nideshop-Token': token
      },
      method:"POST",
      data:{
        addressId: this.data.xianshiaddress[0].id, 
        couponId:"" 
      },
      success(res){
        console.log(res)
        wx.redirectTo({
          url: '/pages/order/order?orderid='+ res.data.data.orderInfo.id,
          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        })
      }
    })
  }


})